#include "csvgicon.h"

#include <buola/gl/cgltexture.h>
#include <buola/image/cimage.h>

namespace buola { namespace bdesktop {

CSVGIcon::CSVGIcon(const CURL &pURL)
    :   mURL(pURL)
{
}

CSVGIcon::~CSVGIcon()
{
}

double CSVGIcon::GetRelativeWidth()
{
    return 1.0;
}

void CSVGIcon::Draw(const CRect &pRect,double pOpacity)
{
    CGLTexture *lTexture=GetTexture(128);//lSize.x);

    lTexture->Enable();

    double lTexArray[8]={0.0,0.0,1.0,0.0,1.0,1.0,0.0,1.0};
    double lVertexArray[8]={pRect.l,pRect.t,pRect.r,pRect.t,
                            pRect.r,pRect.b,pRect.l,pRect.b};

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

    glVertexPointer(2,GL_DOUBLE,0,lVertexArray);
    glTexCoordPointer(2,GL_DOUBLE,0,lTexArray);

    glColor4d(pOpacity,pOpacity,pOpacity,pOpacity);
    glEnable(GL_BLEND);
    glDrawArrays(GL_QUADS,0,4);

    lTexture->Disable();
}

CGLTexture *CSVGIcon::GetTexture(int pSize)
{
    if(mTextures.count(pSize))
        return mTextures[pSize];

    CGLTexture *lTexture=new CGLTexture;
    lTexture->Bind(CImage::GetURL(mURL,CIntSize(pSize,pSize)));

    mTextures[pSize]=lTexture;

    return lTexture;
}

/*namespace bdesktop*/ } /*namespace buola*/ }
